package com.whlilong.jxc.ui.sorter;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

import com.whlilong.jxc.model.Keshang;

public class DistrictSorter extends ViewerSorter {

	// 每列对应一个不同的常量，正数表示要升序、相反数表示要降序

	private static final int ID = 1;

	private static final int ksid = 2;

	private static final int ksarea = 3;

	// 给外界使用排序器对象

	public static final DistrictSorter ID_ASC = new DistrictSorter(ID);

	public static final DistrictSorter ID_DESC = new DistrictSorter(-ID);

	public static final DistrictSorter ksid_asc = new DistrictSorter(ksid);

	public static final DistrictSorter ksid_desc = new DistrictSorter(-ksid);

	public static final DistrictSorter ksarea_asc = new DistrictSorter(ksarea);

	public static final DistrictSorter ksarea_desc = new DistrictSorter(-ksarea);

	// 当前所要排序的列，取自上面的ID、NAME两值或其相反数

	private int sortType;

	// 构造函数用private，表示不能在外部创建MySorter对象

	private DistrictSorter(int sortType) {

		this.sortType = sortType;

	}

	// 最关键的比较方法compare，改写自ViewerSorter。方法返回值是一个int值：正数则

	// obj1移到obj2之前；零则obj1和obj2的位置不动；负数则obj1移到obj2之后

	public int compare(Viewer viewer, Object obj1, Object obj2) {

		// 传入两条记录（实体类），然后依列给出它们的先后顺序

		Keshang o1 = (Keshang) obj1;

		Keshang o2 = (Keshang) obj2;

		switch (sortType) {
			case ID: {
				int l1 = o1.getId();
				int l2 = o2.getId();
				// Long的compareTo方法返回值有三个可能值1,0,-1：
	
				// 如l1＞l2则返回1；如l1＝l2则返回0；如l1＜l2则返回-1
				return l1 > l2 ? 1 : -1;
			}
	
			case -ID: {
				int l1 = o1.getId();
				int l2 = o2.getId();
				return l2 > l1 ? 1 : -1;
			}
			case ksid: {
				String s1 = o1.getKeshangid();
				String s2 = o2.getKeshangid();
				return s1.compareTo(s2);
			}
	
			case -ksid: {
				String s1 = o1.getKeshangid();
				String s2 = o2.getKeshangid();
				return s2.compareTo(s1);
			}
			case ksarea: {
				String s1 = o1.getKeshangarea();
				String s2 = o2.getKeshangarea();
				return s1.compareTo(s2);
			}
			case -ksarea: {
				String s1 = o1.getKeshangarea();
				String s2 = o2.getKeshangarea();
				return s2.compareTo(s1);
			}
		}
		return 0;

	}

}
